import 'package:dio/dio.dart';
import 'package:tuple/tuple.dart';
import 'String+extension.dart';

extension DioExtension on Response {
  Map<String, dynamic> toValue() {
    return toString().toMap();
  }

  Tuple4<Map<String, dynamic>, bool, int, String> toResult() {
    final Map<String, dynamic> j = toValue();
    int ret = int.parse((j['code'] ?? "999").toString());
    String msg = (j['msg'] ?? "数据异常").toString();
    bool success = ret == 200;
    if (!success) {
      UtilLog("request:");
      UtilLog(requestOptions.uri.toString());
      UtilLog("body:");
      UtilLog((requestOptions.data as FormData)
          .fields
          .map((e) => "\"${e.key}\":${e.value}")
          .toList()
          .toString());
    }
    UtilLog("response:");
    UtilLog(toString());
    return Tuple4<Map<String, dynamic>, bool, int, String>(
        j, success, ret, msg);
  }
}
